package com.ziyueyuwen.xiniao.controller.course;

import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ao.CourseSaveAO;
import com.ziyueyuwen.xiniao.bean.page.PageData;
import com.ziyueyuwen.xiniao.bean.query.CourseQuery;
import com.ziyueyuwen.xiniao.bean.vo.ZyCourseVO;
import com.ziyueyuwen.xiniao.enums.CourseQuarterEnum;
import com.ziyueyuwen.xiniao.enums.GradeEnum;
import com.ziyueyuwen.xiniao.enums.ParamTypeEnum;
import com.ziyueyuwen.xiniao.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

import static com.ziyueyuwen.xiniao.base.BaseUtil.*;
import static com.ziyueyuwen.xiniao.util.ConstUtil.PARAM_COURSE_ID;

/**
 * @author wancheng  on 2018/12/17.
 */
@Controller
@Validated
@RequestMapping("/course")
public class CourseController {

    @Autowired
    CourseService courseService;

    /**
     * 获取所有年级
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/get_grade.do", method = RequestMethod.POST)
    public void getGrade(HttpServletRequest request,
                         HttpServletResponse response) {
        HttpResContent<List<Map<String, Object>>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取年级");
        try {
            List<Map<String, Object>> res = GradeEnum.getAllMap();
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    /**
     * 添加课程
     *
     * @param request
     * @param response
     * @param course
     */
    @RequestMapping(value = "/save_course.do", method = RequestMethod.POST)
    public void saveClass(HttpServletRequest request,
                          HttpServletResponse response,
                          @Validated CourseSaveAO course) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("添加课程");
        try {
            courseService.saveCourse(course);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_course.do", method = RequestMethod.POST)
    public void updateClass(HttpServletRequest request,
                            HttpServletResponse response,
                            @Validated CourseSaveAO course) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改课程");
        try {
            if (course.getCourseId() == null) {
                throw new Exception("课程id不能为空");
            }
            courseService.updateCourse(course);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_all_course.do", method = RequestMethod.POST)
    public void getClassInfoByCourseId(HttpServletRequest request,
                                       HttpServletResponse response,
                                       @Validated CourseQuery query) {
        HttpResContent<PageData<ZyCourseVO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取课程列表");
        try {
            PageData<ZyCourseVO> res = courseService.getAllZyCourse(query, query.getPage(), query.getRows());
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/list_trial_course.do", method = RequestMethod.POST)
    public void listTrialCourse(HttpServletRequest request,
                                HttpServletResponse response,
                                @Validated CourseQuery query) {
        HttpResContent<PageData<ZyCourseVO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取体验课课程列表");
        try {
            query.setQuarter(CourseQuarterEnum.COURSE_EXPERIENCE);
            PageData<ZyCourseVO> res = courseService.getAllZyCourse(query, query.getPage(), query.getRows());
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_course_by_id.do", method = RequestMethod.POST)
    public void getClassInfoByCourseId(HttpServletRequest request,
                                       HttpServletResponse response,
                                       @RequestParam(PARAM_COURSE_ID) Integer courseId) {
        HttpResContent<ZyCourseVO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取课程");
        try {
            checkParam(ParamTypeEnum.INTEGER_GT_0, PARAM_COURSE_ID, courseId);
            ZyCourseVO res = courseService.getByCourseId(courseId);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/delete_empty_course.do", method = RequestMethod.POST)
    public void deleteEmptyCourse(@RequestParam(PARAM_COURSE_ID) Integer courseId,
                                  HttpServletRequest request,
                                  HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("删除空课程");
        try {
            // check.
            checkParam(ParamTypeEnum.INTEGER_GT_0, PARAM_COURSE_ID, courseId);
            // do.
            Boolean ret = courseService.deleteEmptyCourseByCourseId(courseId);
            saveSuccessMsgAndData(hrc, ret);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }
}
